package alo;

import org.junit.Test;

/**
 * @ClassName ALI3
 * @Description TODO
 * @Author Fanxc
 * @DATE 2021/6/13 22:47
 * @Version 1.0
 */
public class ALI3 {



    //评测题目: 两个很大的整数，字符串表示，求相减后的结果，也是字符串表示。
    //字符串有正负号，不能用BigDecimal函数，如”123“ - ”124“ ，”123“ - ”-123“
    int c = 0;
    String Del(String s1, String s2) {
        //初始化
        boolean f1 = true;
        boolean f2 = true;
        char[] n1;
        char[] n2;
        if (s1.charAt(0) == '-'){
            f1=false;
            n1=s1.substring(1).toCharArray();

        }else {
            n1 = s1.toCharArray();
        }
        if (s2.charAt(0) == '-'){
            f2=false;
            n2=s2.substring(1).toCharArray();

        }else {
            n2 = s2.toCharArray();
        }
        int l1 = n1.length-1;
        int l2 = n2.length-1;
        int m = Math.max(l1, l2) + 2;
        char[] n = new char[m];
        c = 0;

        //模式判断
        if (f1) {
            if (!f2) {
                //一正一负，即两个正数的加法，结果为正
                if (big(n1,n2)) {
                    add(n1, n2, l1, l2, n, m);
                } else {
                    add(n2, n1, l2, l1, n, m);
                }
            } else {
                //两正，即两个正数的减法，结果的正负，看两个值的大小
                if (big(n1,n2)) {
                    del(n1, n2, l1, l2, n, m);
                } else {
                    del(n2, n1, l2, l1, n, m);
                    n[0] = '-';
                }
            }
        } else {
            if (!f2) {
                //两负，即两个负数的减法，结果的正负看两个数的大小
                if (big(n1,n2)) {
                    del(n1, n2, l1, l2, n, m);
                    n[0] = '-';
                } else {
                    del(n2, n1, l2, l1, n, m);
                }
            } else {
                //一负一正，即两个负数的加法，结果为负
                if (big(n1,n2)) {
                    add(n1, n2, l1, l2, n, m);
                } else {
                    add(n2, n1, l2, l1, n, m);
                }
                n[0] = '-';
            }

        }
        return String.valueOf(n).toString();
    }

    //加法
    void add(char[] n1, char[] n2, int l1, int l2, char[] n, int m) {
        while (l1 >= 0 && l2 >= 0) {
            int a = n1[l1] - '0';
            int b = n2[l2] - '0';
            n[--m] = (char) ((a + b + c) % 10+'0');
            l1--;
            l2--;
            c = (a + b + c) / 10;
        }
        while (l1 >= 0) {
            int a = n1[l1] - '0';
            int b = 0;
            n[--m] = (char) ((a + b + c) % 10+'0');
            l1--;
            c = 0;
        }
        if (c == 1) n[0] = '1';
    }

    //减法
    void del(char[] n1, char[] n2, int l1, int l2, char[] n, int m) {
        while (l1 >= 0 && l2 >= 0) {
            int a = n1[l1] - '0' - c;
            if (a < 0) {
                n1[l1 - 1] -= 1;
                a+=10;
            }
            int b = n2[l2] - '0';
            n[--m] =(char) ((a - b +10) % 10+'0');
            l1--;
            l2--;
            c = a >= b ? 0 : 1;
        }
        while (l1 >= 0) {
            int a = n1[l1] - '0' - c;
            if (a < 0) {
                n1[l1 - 1] -= 1;
                a+=10;
            }
            c=0;
            int b = 0;
            n[--m] = (char) ((a - b+10) % 10+'0');
            l1--;
        }
    }
    //比较大小
    boolean big(char[] n1, char[] n2) {
        int l1 = n1.length - 1;
        int l2 = n2.length - 1;
        if (l1>l2){
            return true;
        }else if (l1<l2){
            return false;
        }else {
            for (int i = 0; i <= l1; i++) {
                if (n1[i]>n2[i]){
                    return true;
                }
                if (n1[i]<n2[i]){
                    return false;
                }
            }
        }
        return true;
    }

    @Test
    public void test(){
        String s1="-994";
        String s2="1003";
        String s3=Del(s1,s2);
        System.out.println(s3);
    }
}